
/* Generated by Interface Builder */

#import "ExpMenuView.h"
#import <dpsclient/wraps.h>
#import <appkit/color.h>
#import "MenuItems.h"
#import "ExpController.h"


@implementation ExpMenuView

- initFrame: (NXRect *) frameRect
{
    state = 0;
    
    [super initFrame:frameRect];
    
    return self;
}

- drawStartSquare
{
    NXRect rect;
    
    [self getFrame:&rect];   /* get view size */
    
    menu_rect( rect.size.width/2.0-10.0, rect.size.height/2.0+10.0, 20.0,
               20.0);
    PSgsave(); PSsetgray(0.5); PSfill(); PSgrestore();
    PSsetgray(0.0); PSstroke();
 
    return self;
}


- drawSelf: (NXRect *) drawRects :(int) rectCount
{
    NXEraseRect(&bounds);
    
    if ( state == START_SQUARE )
        [self drawStartSquare];
    else if ( state == SHOW_MENU )
        [super drawSelf:drawRects :rectCount];
	
    return self;
}    

- hitTestVerticalMenu:(NXPoint) point
{
    int i;
    float y;
    float x_center;
    float item_height, item_width, item_distance;
    int hit_flag = 0;
    NXRect rect;
    
    [self convertPoint:&point fromView:NULL];
    
    /* converts item millimeters to PS coord values */
    item_width = MM_CONVERT * option1;
    item_height  = MM_CONVERT * option2;
    item_distance = MM_CONVERT * option3;

    [self getFrame:&rect];   /* get view size */
    x_center = (rect.size.width / 2.0) - (item_width / 2.0);
    
    y = rect.size.height/2.0 + (numItems *(item_distance+item_height)) / 2.0;
    
    for ( i = 0; i < numItems; i++, y -= (item_distance+item_height) )
    {
	menu_rect(x_center, y, item_height, item_width);
	
	PSinfill(point.x, point.y, &hit_flag);
	
	if ( hit_flag )
	{
	    fprintf(stderr, "Hit menu item %d!\n", i);
	    [expController menuItemHit:i];
	    break;
	}

    }
    
    if ( !hit_flag )
        [expController menuItemMissed];        
	
    return self;
}

- hitTestCirclePieMenu:(NXPoint) point
{
    int i;
    float x_center, y_center;
    NXRect rect;
    int hit_flag = 0;
    float angle = 360 / (float) numItems;

    [self convertPoint:&point fromView:NULL];
    
    [self getFrame:&rect];   /* get view size */
    x_center = rect.size.width / 2.0;
    y_center = rect.size.height / 2.0;
    
    for ( i = 0, angle = 0.0; i < numItems;
          i++, angle += 360/(float) numItems )
    {
        menu_pie(x_center, y_center, angle, angle+option1,
	         MM_CONVERT * option2, MM_CONVERT*(option2+option3));
	
	PSinfill(point.x, point.y, &hit_flag);
	
	if ( hit_flag )
	{
	    fprintf(stderr, "Hit menu item %d\n", i);
	    [expController menuItemHit:i];
	    break;
	}	 
    }
    
    if ( !hit_flag )
    {
        fprintf(stderr, "No item hit, x:%f, y:%f\n", point.x, point.y);
	[expController menuItemMissed];
    }
    
    return self;
}

- hitTestCircleRectMenu:(NXPoint) point
{
    int i;
    NXRect rect;
    float x_center, y_center;
    int hit_flag = 0;
    float angle = 360 / (float) numItems;
    
    [self convertPoint:&point fromView:NULL];
    
    [self getFrame:&rect];   /* get view size */
    x_center = rect.size.width / 2.0 - (MM_CONVERT * option1);
    y_center = rect.size.height / 2.0;
    
    for ( i = 0, angle = 0.0; i < numItems;
          i++, angle += 360/(float) numItems )
    {
        menu_pie_rect(x_center, y_center, MM_CONVERT*option3, angle,
	               MM_CONVERT * option1, MM_CONVERT*option2);
	
	PSinfill(point.x, point.y, &hit_flag);

	if ( hit_flag )
	{
	    fprintf(stderr, "Hit menu item %d\n", i);
	    [expController menuItemHit:i];
	    break;
	}
    }
    
    if ( !hit_flag )
    {
        fprintf(stderr, "No item hit, x:%f, y:%f\n", point.x, point.y);
	[expController menuItemMissed];
    }

    return self;
}

- hitTestStartSquare:(NXPoint) point
{
    int hit_flag = 0;
    NXRect rect;
    
    [self convertPoint:&point fromView:NULL];
    [self getFrame:&rect];   /* get view size */

    menu_rect(rect.size.width/2.0-10.0, rect.size.height/2.0+10.0, 20.0, 20.0);

    PSinfill(point.x, point.y, &hit_flag);
    
    if ( hit_flag )
    {
        fprintf(stderr, "StartSquare hit.\n");
	[expController startSquareHit];
	
	state = SHOW_MENU;
	[self display];
    }
    else
        fprintf(stderr, "StartSquare missed.\n");

    return self;
}


- mouseDown:(NXEvent *) event
{
    if ( event->type == NX_LMOUSEDOWN )
        if ( state == START_SQUARE )
	    [self hitTestStartSquare:event->location];
    
    return self;
}

- mouseUp:(NXEvent *) event
{
    if ( event->type == NX_LMOUSEUP )
    {
	if ( state == SHOW_MENU )
	{
	    state = 0;
	    
            switch ( menuType )
	    {
	        case VERTICAL:
	            [self hitTestVerticalMenu:event->location];
                    break;
		
		case CIRCLE_PIE:
		    [self hitTestCirclePieMenu:event->location];
		    break;
		    
		case CIRCLE_RECTANGLE:
		    [self hitTestCircleRectMenu:event->location];
		    break;
	    }
	}
    }
    
    return self;
}


- setStartState
{
fprintf(stderr, "setStartState\n");

    state = START_SQUARE;
    [self display];
    
    return self;
}
  
- setMenuParms:(MVParms *) parms
{
    numItems = parms->num_items;
    menuType = parms->menu_type;
    option1  = parms->option1;
    option2  = parms->option2;
    option3  = parms->option3;
    unselectedColour = parms->unselected_colour;
    selectedColour   = parms->selected_colour;

    return self;
}



@end
